//package com.cgly.medical.filter;
//
//import com.cgly.medical.entity.AccountModel;
//import com.cgly.medical.util.JwtUtils;
//import io.jsonwebtoken.ExpiredJwtException;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.util.StringUtils;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.annotation.Resource;
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.List;
//
///**
// * 通过请求头发送过来的token查询用户信息并判断用户身份并赋予此次用户访问的权限
// * @program: medical
// * @description:
// * @author: chen di
// * @create: 2022-08-15 15:48
// */
//@Configuration
//public class JwtFilter extends OncePerRequestFilter {
//    @Resource
//    UserDetailsService userDetailsService;
//
//    /**
//     * 通过请求头发送过来的token查询用户信息并判断用户身份并赋予此次用户访问的权限
//     * @param request
//     * @param response
//     * @param filterChain
//     * @throws ServletException
//     * @throws IOException
//     */
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String token = request.getHeader("Authorization");
//        response.setContentType("text/json;charset=utf-8");
//        if(StringUtils.hasLength(token)) {
//            String uname = null;
//            try {
//                uname = (String) JwtUtils.getClaims(token).get("uname");
//                System.out.println(uname);
//            } catch (ExpiredJwtException e){
//                logger.info("失效身份");
//            }
//            if(uname != null){
//                String role = (String) JwtUtils.getClaims(token).get("role");
//                UserDetails userDetails = userDetailsService.loadUserByUsername("uname");
//                AccountModel model = (AccountModel) userDetails;
//                List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(role);
//                UsernamePasswordAuthenticationToken authenticationFilter = new UsernamePasswordAuthenticationToken(model, null, grantedAuthorities);
//
//                authenticationFilter.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//                SecurityContextHolder.getContext().setAuthentication(authenticationFilter);
//            }
//        }
//        filterChain.doFilter(request,response);
//    }
//}